import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
/*import Mock from './mock'
Mock.bootstrap();*/
import 'font-awesome/css/font-awesome.min.css'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)
/*导入axios*/
import axios from 'axios'
//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
    if (to.path == '/login' || localStorage.getItem('TOKEN')) {
        next()
    } else {
        alert('请登录')
        next('/login')
    }
})

//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

axios.interceptors.request.use(
    config => {
        //config相当于所有请求
        //取得token
        let token=localStorage.getItem("TOKEN")
        if(token){//不为空设置到请求头上
            config.headers["U-TOKEN"]=token;
        }
        return config
    },
    err => {
        return Promise.reject(err)
    })

axios.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
}, function (error) {
    // Do something with response error
    return Promise.reject(error);
});
/*
//前端拦截请求
// http request 拦截器
axios.interceptors.request.use(
    config => {
      //config相当于所有请求
      //取得token
      let token=localStorage.getItem("TOKEN")
      if(token){//不为空设置到请求头上
        config.headers["U-TOKEN"]=token;
      }
      return config
    },
    err => {
      return Promise.reject(err)
    })
*/

/*
// http response 拦截器
axios.interceptors.response.use(
    response => {
      console.log(response);
      //拦截响应，做统一处理
      if(!response.data.success&&response.data.message==="noAuth"){//拦截
        location.href="login.html"
        return ;//直接结束ser
      }
      return response
    },
    //接口错误状态处理，也就是说无响应时的处理
    error => {
      return Promise.reject(error.response.status) // 返回接口返回的错误信息
    })
*/


